package com.smmisia.shop.controller;


import com.alicp.jetcache.anno.CacheRefresh;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.smmisia.common.Result.Result;
import com.smmisia.common.response.PageResponse;
import com.smmisia.shop.model.ProductVO;
import com.smmisia.shop.request.*;
import com.smmisia.shop.response.ProductCreateResponse;
import com.smmisia.shop.response.ProductInventoryModifyResponse;
import com.smmisia.shop.response.ProductPriceModifyResponse;
import com.smmisia.shop.response.ProductRemoveResponse;
import com.smmisia.shop.service.ProductInventoryStreamService;
import com.smmisia.shop.service.ProductService;
import com.smmisia.shop.threadLocal.LoginUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/product")
@Slf4j
@Tag(name = "商品管理")
public class ProductController {

    @Autowired
    private ProductService productService;



    /**
     * 新增商品
     *
     * @param request
     * @return
     */
    @PostMapping("/save")
    @Operation(summary = "新增商品")
    public Result<Integer> save(@Valid @RequestBody ProductCreateRequest request) {
        return Result.ok(productService.saveProduct(request).getProductId());
    }

    /**
     * 商品分页查询
     *
     * @param request
     * @return
     */
    @PostMapping("/page")
    @Operation(summary = "商品分页查询")
    public Result<PageResponse<ProductVO>> pageQuery(@Valid @RequestBody ProductPageQueryRequest request) {
        return Result.ok(productService.pageQuery(request));
    }

    /**
     * 修改库存
     *
     * @param request
     * @return
     */
    @PostMapping("/updateInventory")
    @Operation(summary = "修改库存")
    public Result<ProductInventoryModifyResponse> update(@Valid @RequestBody ProductInventoryModifyRequest request) {
        return Result.ok(productService.modifyInventory(request));
    }


    /**
     * 修改价格
     *
     * @param request
     * @return
     */
    @PostMapping("/updatePrice")
    @Operation(summary = "修改价格")
    public Result<ProductPriceModifyResponse> modifyPrice(@Valid @RequestBody ProductModifyPriceRequest request) {
        return Result.ok(productService.modifyPrice(request));
    }


    /**
     * 查询
     *
     * @param productId
     * @return
     */
    @GetMapping("/query")
    @Cached(name = "product:cache:id:", expire = 60, localExpire = 10, timeUnit = TimeUnit.MINUTES, cacheType = CacheType.BOTH, key = "#productId", cacheNullValue = true)
    @CacheRefresh(refresh = 50, timeUnit = TimeUnit.MINUTES)
    @Operation(summary = "查询商品")
    public Result<ProductVO> queryById(@RequestParam("id") Long productId) {
        return Result.ok(productService.queryById(productId));
    }

    /**
     * 下架商品
     *
     * @param request
     * @return
     */
    @PostMapping("/remove")
    @Operation(summary = "下架商品")
    public Result<ProductRemoveResponse> deleteProduct(@Valid @RequestBody ProductRemoveRequest request) {
        return Result.ok(productService.removeProduct(request));
    }


}
